import {StatusBar} from "@ionic-native/status-bar";
import {Injectable} from '@angular/core';
import {AlertController, Animation, LoadingController} from 'ionic-angular';
import {Storage} from '@ionic/storage';
import {AlertOptions} from "ionic-angular/components/alert/alert-options";
import {PopoverTransition} from "ionic-angular/components/popover/popover-transitions";

@Injectable()
export class Helper {
  static sb: StatusBar = new StatusBar()
  static eventManage = {
    handles: {},
    on(eName, fn) {
      this.handles[eName] = this.handles[eName] ? this.handles[eName] : []
      this.handles[eName].push(fn);
    },
    emit(eName, data = {}) {
      if (this.handles[eName]) {
        for (var i = 0; i < this.handles[eName].length; i++) {
          this.handles[eName][i](data);
        }
      }
    },
    off: function (eventType, handler = null) {
      if (handler === null) {
        delete this.handles[eventType]
        return
      }
      let currentEvent = this.handles[eventType];
      let len = 0;
      if (currentEvent) {
        len = currentEvent.length;
        for (let i = len - 1; i >= 0; i--) {
          if (currentEvent[i] === handler) {
            currentEvent.splice(i, 1);
          }
        }
      }
    }
  }

  private loader: any

  constructor(public alertCtrl: AlertController,
              public storage: Storage,
              public loadingCtrl: LoadingController) {
    // this.storage.forEach((value, key, iterationNumber)=>{
    //   console.info(key,value)
    // })
  }

  static setStatusBarDefault() {
    // this.sb.styleDefault()
    this.sb.styleLightContent()
    this.sb.backgroundColorByHexString('#ff000000')
  }

  static setStatusBarTranslucence() {
    this.sb.styleLightContent()
    this.sb.backgroundColorByHexString('#55000000')
  }

  alert(options:AlertOptions) {
    let alert = this.alertCtrl.create({
      cssClass: options.cssClass ? options.cssClass : 'g-alert',
      mode: 'ios',
      enableBackdropDismiss: !!options.enableBackdropDismiss,
      title: options.title,
      buttons: options.buttons
    });
    alert.present();
  }

  loading(text = '',transparent=true,full=true) {
    let cssClass = ''
    cssClass += transparent?' transparent':''
    cssClass += full?'':' not-full'
    this.loader = this.loadingCtrl.create({
      content: text,
      cssClass: cssClass
    });
    this.loader.present();
  }

  stopLoading() {
    this.loader && this.loader.dismissAll()
    this.loader = null
  }

  setStorage(key: string, value: string) {
    return this.storage.set(key, value)
  }

  getStorage(key: string) {
    return this.storage.get(key)
  }

  removeStorage(key: string) {
    return this.storage.remove(key)
  }

  checkLogin(success = () => {
  }, fail = () => {
  }) {
    return this.getStorage('user/login').then(d => {
      if (d == '1') {
        success()
      } else {
        fail()
      }
    })
  }
}

export class GPopoverPopIn extends PopoverTransition {
  init() {
    let platformH = window.innerHeight
    let finalTop = platformH - 270
    let ele = this.enteringView.pageRef().nativeElement;
    let wrapper = new Animation(this.plt, ele.querySelector('.popover-content'));

    wrapper.fromTo('top', platformH + 'px', finalTop + 'px');

    this
      .easing('easeOut')
      .duration(300)
      .add(wrapper);
  }

  play() {
    this.plt.raf(() => {
      this.iosPositionView(this.enteringView.pageRef().nativeElement, this.opts.ev);
      super.play();
    });
  }
}

export class GPopoverPopOut extends PopoverTransition {
  init() {
    let platformH = window.innerHeight
    let finalTop = platformH - 270
    let ele = this.leavingView.pageRef().nativeElement;
    let wrapper = new Animation(this.plt, ele.querySelector('.popover-content'));

    wrapper.fromTo('top', finalTop + 'px', platformH + 'px');

    this
      .easing('easeOut')
      .duration(300)
      .add(wrapper);
  }
}
